package com.flyxiaozhu.erp.admin.controllers;

import com.flyxiaozhu.erp.core.entities.GoodsCategory;
import com.flyxiaozhu.erp.core.libs.ErrorCode;
import com.flyxiaozhu.erp.core.libs.JSONResult;
import com.flyxiaozhu.erp.core.repositories.GoodsCategoryRepository;
import com.flyxiaozhu.erp.core.services.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: @Faith
 * Date: 2018/7/24
 * Time: 下午3:20
 */
@RestController
public class GoodsCategoryController {

    @Autowired
    GoodsCategoryRepository goodsCategoryRepository;


    @Autowired
    GoodsCategoryService goodsCategoryService;


    @Secured("ROLE_GOODS_CATEGORY_SELECT")
    @RequestMapping(value = "/goodsCategory/select")
    public JSONResult selectCategory() {

        List<Map> cateTree = goodsCategoryService.getFormattedTypes(goodsCategoryService.getTree2());

        return new JSONResult().put("list", cateTree).put("total", cateTree.size());
    }

    @RequestMapping(value = "/goodsCategory/select2")
    public JSONResult selectCategoryByLevel() {

        List<Map> cateTree = new ArrayList<>();

        Map<String, String> parent = new HashMap<>();
        parent.put("id", "0");
        parent.put("name", "父分类");

        cateTree.add(parent);

        cateTree.addAll(goodsCategoryService.getFormattedTypes(goodsCategoryService.getTree3(2)));

        return new JSONResult().put("list", cateTree).put("total", cateTree.size());
    }

    @Secured("ROLE_GOODS_CATEGORY_INSERT")
    @RequestMapping(value = "/goodsCategory/insert")
    public JSONResult add(GoodsCategory goodsCategory) {
        goodsCategoryService.save(goodsCategory);
        return new JSONResult().put("model", goodsCategory);
    }


    @Secured("ROLE_GOODS_CATEGORY_UPDATE")
    @RequestMapping(value = "/goodsCategory/update")
    @Transactional(rollbackFor = Exception.class)
    public JSONResult update(GoodsCategory goodsCategory) {
        if (goodsCategory.getId() == 0) {
            goodsCategoryService.save(goodsCategory);
            return new JSONResult().put("count", 1).put("model", goodsCategory);
        }

        GoodsCategory oldCategory = goodsCategoryService.findById(goodsCategory.getId());
        if (oldCategory == null) {
            return new JSONResult(ErrorCode.NO_DATA, "商品分类id错误");
        }

        if (goodsCategoryService.hasRing(goodsCategory)) {
            return new JSONResult(ErrorCode.NO_DATA, "分类死循环了，请重新设置");
        }

        goodsCategoryService.update(goodsCategory, oldCategory);

        return new JSONResult().put("count", 1).put("model", goodsCategory);
    }

//    @Secured("ROLE_GOODS_CATEGORY_DELETE")
//    @RequestMapping(value = "/goodsCategory/delete")
//    @Transactional(rollbackFor = Exception.class)
//    public JSONResult delete(@RequestParam String keys) {
//        List<Long> ids = CollectionUtils.map(keys.split(","), o -> Long.parseLong(o));
//
//        List<GoodsCategory> goodsCategoryList = goodsCategoryRepository.findByIdIn(ids);
//        if (goodsCategoryList.size() != ids.size()) {
//            return new JSONResult(ErrorCode.NO_DATA, "商品分类id错误");
//        }
//
//        goodsCategoryService.delete(goodsCategoryList);
//
//        return new JSONResult().put("count", 1).put("model", goodsCategoryList);
//    }
}
